package com.zhihuishu.micro.course.openapi.cc.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.zhihuishu.micro.course.model.MeetCourse;
import com.zhihuishu.micro.course.openapi.cc.MeetCourseOpenService;
import com.zhihuishu.micro.course.openapi.cc.dto.MeetCourseOpenDto;
import com.zhihuishu.micro.course.service.MeetCourseService;
import com.zhihuishu.remote.RemoteException;
import com.zhihuishu.remote.RemoteResult;
import com.zhihuishu.toolkit.helper.BeanHelper;

/** 
 * @author yulijin 
 * @version 创建时间：2016年12月7日 下午1:29:15 
 * 类说明 
 */
@Service("meetCourseOpenServiceForCC")
public class MeetCourseOpenServiceImpl implements MeetCourseOpenService {
	
	@Autowired
	private MeetCourseService meetCourseService;
	
	@Override
	public RemoteResult<List<MeetCourseOpenDto>> getMeetCourseList(Long courseId) throws RemoteException {
		
		RemoteResult<List<MeetCourseOpenDto>> result = new RemoteResult<List<MeetCourseOpenDto>>();
		
		try {
			List<MeetCourse> list = meetCourseService.getMeetCourseList(courseId);
			
			List<MeetCourseOpenDto> meetList = new ArrayList<MeetCourseOpenDto>();
			
			if (!CollectionUtils.isEmpty(list)){
				for(MeetCourse meetCourse : list){
					MeetCourseOpenDto meetDto = BeanHelper.transTo(meetCourse,MeetCourseOpenDto.class);
					meetDto.setSpeakerStr(meetCourse.getSpeakerName());
					meetList.add(meetDto);
				}
			}
			
			result.setResult(meetList);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setResult(null);
			result.setSuccess(false);
			result.setExceptionStack(e);
		}
		
		return result;
	}

	@Override
	public RemoteResult<MeetCourseOpenDto> getMeetCourse(Long meetCourseId) throws RemoteException {
		
		RemoteResult<MeetCourseOpenDto> result = new RemoteResult<MeetCourseOpenDto>();
		
		try {
			MeetCourse meetCourse = meetCourseService.getMeetCourse(meetCourseId);
			
			if (null != meetCourse) {
				MeetCourseOpenDto meetDto = BeanHelper.transTo(meetCourse,MeetCourseOpenDto.class);
				meetDto.setSpeakerStr(meetCourse.getSpeakerName());
				result.setResult(meetDto);
			} else {
				result.setResult(null);
			}
			
			result.setSuccess(true);
		} catch (Exception e) {
			result.setResult(null);
			result.setSuccess(false);
			result.setExceptionStack(e);
		}
		
		return result;
	}

	@Override
	public RemoteResult<Integer> findMeetCourseCount(Long courseId) throws RemoteException {
		
		RemoteResult<Integer> result = new RemoteResult<Integer>();
		
		try {
			Integer count = meetCourseService.findMeetCourseCount(courseId);
			result.setResult(count);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setResult(null);
			result.setSuccess(false);
			result.setExceptionStack(e);
		}
		
		return result;
	}

	@Override
	public RemoteResult<MeetCourseOpenDto> insert(MeetCourseOpenDto meetCourse) throws RemoteException {
		
		RemoteResult<MeetCourseOpenDto> result = new RemoteResult<MeetCourseOpenDto>();
		
		try {
			MeetCourse meet = BeanHelper.transTo(meetCourse,MeetCourse.class);
			meet.setSpeakerName(meetCourse.getSpeakerStr());
			MeetCourse meetcourse = meetCourseService.insert(meet);
			result.setSuccess(true);
			result.setResult(BeanHelper.transTo(meetcourse,MeetCourseOpenDto.class));
		} catch (Exception e) {
			result.setSuccess(false);
			result.setResult(null);
			result.setExceptionStack(e);
			e.printStackTrace();
		}
		
		return result;
	}

	@Override
	public RemoteResult<Boolean> remove(Long meetCourseId) throws RemoteException {
		
		RemoteResult<Boolean> result = new RemoteResult<Boolean>();
		
		try {
			meetCourseService.delete(meetCourseId);
			result.setSuccess(true);
			result.setResult(true);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setResult(false);
			result.setExceptionStack(e);
		}
		
		return result;
	}

	@Override
	public RemoteResult<MeetCourseOpenDto> update(MeetCourseOpenDto meetCourse) throws RemoteException {
		RemoteResult<MeetCourseOpenDto> result = new RemoteResult<MeetCourseOpenDto>();
		
		try {
			MeetCourse meet = BeanHelper.transTo(meetCourse,MeetCourse.class);
			meet.setSpeakerName(meetCourse.getSpeakerStr());
			meetCourseService.update(meet);
			MeetCourseOpenDto meetCourseDto = BeanHelper.transTo(meetCourseService.getMeetCourse(meet.getMeetCourseId()),MeetCourseOpenDto.class);
			result.setSuccess(true);
			result.setResult(meetCourseDto);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setResult(null);
			result.setExceptionStack(e);
		}
		
		return result;
	}
}
